#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <qdialog.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qprocess.h>
#include <qtextedit.h>
#include <qtimer.h>
#include <sambamanager.h>

#include <QCursor>
#include <QDebug>
#include <QFileSystemModel>
#include <QFileSystemWatcher>
#include <QTreeView>

#include <widgets/sambaconfigdialog.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    reload();

    ui->filepath->setText("");
    ui->version->setText("");
    ui->path->setText("");
    ui->comment->setText("");
    ui->usershare_acl->setText("");
    ui->guest_ok->setText("");
    ui->sharename->setText("");


    auto watcher = new QFileSystemWatcher;
    watcher->addPath(var_lib_samba_usersahres);
    connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(reload()));

    QTimer::singleShot(1000, [this](){
        {
            QProcess process;
            process.start("groups");
            process.waitForFinished();
            bool sambashared = process.readAllStandardOutput().contains("sambashare");

            if (!sambashared) {
                QMessageBox::warning(this, "提示", "您还没有加入到 sambashare 用户组。");

                QTextEdit *textEdit = new QTextEdit(this);
                textEdit->setText("请执行以下命令加入 sambashare 用户组：\n\n"
                                   "sudo usermod -a -G sambashare $USER\n\n"
                                   "然后重新登录。");
                QDialog dialog;
                QLayout *layout = new QVBoxLayout;
                layout->addWidget(textEdit);
                dialog.setLayout(layout);
                dialog.exec();
            }
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::reload()
{
    SambaManager manager;

    ui->listWidget->clear();

    for (auto &var : manager.usershares())
    {
        ui->listWidget->addItem(var);
    }
}

void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
    ui->textBrowser->clear();

    if (item == nullptr)
        return;

    auto var = item->text();

    QString var_path = var_lib_samba_usersahres + var;

    QFile var_file(var_path);
    var_file.open(QFile::ReadOnly);

    auto var_content = var_file.readAll();
    qDebug() << var_content;
    QTextStream(stdout) << var_content;

    SambaShare share(var_content);

    qDebug() << share.version();
    qDebug() << share.path();
    qDebug() << share.comment();
    qDebug() << share.usershare_acl();
    qDebug() << share.guest_ok();
    qDebug() << share.sharename();

    ui->filepath->setText(var_path);
    ui->version->setText(share.version());
    ui->path->setText(share.path());
    ui->comment->setText(share.comment());
    ui->usershare_acl->setText(share.usershare_acl());
    ui->guest_ok->setText(share.guest_ok());
    ui->sharename->setText(share.sharename());

    ui->textBrowser->setText(var_content.trimmed());

    // 显示共享文件夹内容
    m_tree_model.setRootPath(share.path());

    ui->treeView->setModel(&m_tree_model);
    ui->treeView->setRootIndex(m_tree_model.index(share.path()));
}


void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu;

    if (ui->listWidget->currentItem() != nullptr) {
        menu.addAction("删除共享", [this](){
            SambaManager manager;
            auto sharename = ui->listWidget->currentItem()->text();
            manager.remove(sharename);
            reload();
        });
    }

    menu.addAction("添加共享", [this](){
        SambaConfigDialog dialog;
        if (dialog.exec() == QDialog::Accepted) {
            SambaManager manager;
            manager.create(dialog.config());
            reload();
        }
    });

    menu.exec(QCursor::pos());
}

